package com.example.newnoke.fragment.main;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.widget.AdapterView;

import com.example.newnoke.APP;
import com.example.newnoke.R;
import com.example.newnoke.adapter.CommonAdapter;
import com.example.newnoke.adapter.ViewHolder;
import com.example.newnoke.data.ImListDatas;
import com.example.newnoke.data.UserData;
import com.example.newnoke.databinding.FragmentImBinding;
import com.example.newnoke.mvp.presenter.UserPresenter;
import com.example.newnoke.mvp.view.IUserV;
import com.example.newnoke.ui.EamingsActivity;
import com.example.newnoke.ui.FTActivity;
import com.example.newnoke.ui.GYActivity;
import com.example.newnoke.ui.InfoActivity;
import com.example.newnoke.ui.LoginActivity;
import com.example.newnoke.ui.TiXianActicity;
import com.example.newnoke.ui.WebActivity;
import com.example.newnoke.ui.YeJiActivity;
import com.example.newnoke.ui.YeJiActivity2;
import com.example.newnoke.utils.API;
import com.geya.jbase.basefragment.BaseDetailsFragment;
import com.geya.jbase.constant.RequestType;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Administrator on 2018/5/22 0022.
 */

public class ImFragment extends BaseDetailsFragment<UserPresenter> implements IUserV {

    private FragmentImBinding mBinding;
    private String url;
    private String img;


    @Override
    public UserPresenter newPresenter() {
        return new UserPresenter(this);
    }

    @Override
    public int inflateCreateView() {
        return R.layout.fragment_im;
    }

    @Override
    public void initDatas(View view) {
        mBinding = DataBindingUtil.bind(view);
        mBinding.exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                APP.saveUserInfo("");
                Intent intent = new Intent(getActivity(), LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        mBinding.tixian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),TiXianActicity.class)
                .putExtra("yue",yue)
                );
            }
        });
        initAdapter();
    }

    @Override
    protected void onFragmentStartLazy() {
        super.onFragmentStartLazy();
        HashMap<String, String> map = new HashMap<>();
        mPresenter.accessServers(RequestType.OKGO_POST, API.USER, UserData.class, map);
    }



    public void initAdapter() {


        List<ImListDatas> list2 = new ArrayList<>();

        list2.add(new ImListDatas(R.mipmap.icon_img_8, "复投", "0"));
        list2.add(new ImListDatas(R.mipmap.icon_img_5, "个人信息", "0"));
        list2.add(new ImListDatas(R.mipmap.icon_img_6, "邀请好友", "0"));
        list2.add(new ImListDatas(R.mipmap.icon_img_7, "关于我们", "0"));
        CommonAdapter<ImListDatas> adapter2 = new CommonAdapter<ImListDatas>(getActivity(), list2, R.layout.item_list_im_list) {
            @Override
            public void convert(ViewHolder holder, ImListDatas s, int position) {
                holder.setText(R.id.name, s.getName())
                        .setImgeSrc(R.id.img, s.getImg());
            }
        };
        mBinding.list.setAdapter(adapter2);
        mBinding.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(getActivity(), FTActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(getActivity(), InfoActivity.class));
                        break;
                    case 2:
//
//                        startActivity(new Intent(getActivity(), InviteActivity.class)
//                        .putExtra("url",url)
//                        );

                        JSONObject json = new JSONObject();
                        try {
                            json.put("shareUrl",url);
                            json.put("qrcodeImg",img);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

//                        json.optString(url);
//                        json.optString(img);

//                        System.out.println("----------------------"+json.toString());
                        startActivity(new Intent(getActivity(), WebActivity.class)
                                .putExtra("id",json.toString())
                                .putExtra("title","邀请好友3")
                                .putExtra("url","file:///android_asset/myshare.html")

                        );
                        break;
                    case 3:
                        startActivity(new Intent(getActivity(), GYActivity.class));


                        break;

                }
//                startActivity(new Intent(getActivity(),SucceedActivity.class)); //购买成功
//                startActivity(new Intent(getActivity(),TiXianActicity.class)); //提现

            }
        });
    }

private String yue="";
    @Override
    public void setData(final UserData.DataBean data) {
        yue = data.getOverage();
//        mBinding.name.setText(data.getName());
        mBinding.shouji.setText(data.getMobile());
        mBinding.zcb.setText("￥" + data.getAmount());
        mBinding.yue.setText("￥" + data.getOverage());
        url = data.getShareUrl();
        img = data.getQrcodeImg();
        List<ImListDatas> list1 = new ArrayList<>();
        list1.add(new ImListDatas(R.mipmap.icon_img_1, "我的股权", "￥" + data.getBonus_amount()));
        list1.add(new ImListDatas(R.mipmap.icon_img_2, "分享奖", "￥" + data.getShare_amount()));
        list1.add(new ImListDatas(R.mipmap.icon_img_3, "团队激励", "￥" + data.getTeam_amount()));
        list1.add(new ImListDatas(R.mipmap.icon_img_4, "我的业绩", "￥" + data.getTotal_business()));
        list1.add(new ImListDatas(R.mipmap.icon_img_4, "罗科链业绩", "￥" + data.getYeji_chain()));


        CommonAdapter<ImListDatas> adapter = new CommonAdapter<ImListDatas>(getActivity(), list1, R.layout.item_list_im_grid) {
            @Override
            public void convert(ViewHolder holder, ImListDatas s, int position) {
                holder.setText(R.id.name, s.getName())
                        .setText(R.id.price, s.getPrice())
                        .setImgeSrc(R.id.img, s.getImg());
            }
        };
        mBinding.gridview.setAdapter(adapter);

        mBinding.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {

                    case 0:
                        startActivity(new Intent(getActivity(), EamingsActivity.class)
                                .putExtra("type", "1")
                                .putExtra("price",data.getBonus_amount())
                                .putExtra("name","我的股权")
                        );
                        break;
                    case 1:
                        startActivity(new Intent(getActivity(), EamingsActivity.class)
                                .putExtra("type", "3")
                                .putExtra("price",data.getShare_amount())
                                .putExtra("name","分享奖")
                        );
                        break;
                    case 2:
                        startActivity(new Intent(getActivity(), EamingsActivity.class)
                                .putExtra("type", "4")
                                .putExtra("price",data.getTeam_amount())
                                .putExtra("name","团队激励")
                        );
                        break;
                    case 3:
                        startActivity(new Intent(getActivity(), YeJiActivity.class)
                                .putExtra("price",data.getTotal_business()+"")

                        );
                        break;
                        case 4:
                        startActivity(new Intent(getActivity(), YeJiActivity2.class)
                                .putExtra("price",data.getYeji_chain()+"")

                        );
                        break;

                }
            }
        });
    }

    @Override
    public void showMsg(int code, String msg) {

    }

}
